package cn.mifengkong.appcommon.smsverify;

import com.mifengkong.frtools.system.timer.FRTimer;
import com.mifengkong.frtools.util.FRString;

/**
 * Created by jack on 2017/11/6
 */

public class SMSVerify {

    private FRTimer frTimer;
    private int totalCount;
    private int count;

    private OnSMSTimeListener onSMSTimeListener;

    public SMSVerify(int count) {
        frTimer = new FRTimer();
        this.totalCount = count;
    }

    public void startCountDown() {
        if (null != frTimer && totalCount > 0) {
            count=totalCount;
            frTimer.schedule(1000, 1000, 60, new Runnable() {
                @Override
                public void run() {
                    count--;
                    if (count <= 0) {
                        if (null != onSMSTimeListener) {
                            onSMSTimeListener.onNotReceiveSMS();
                        }
                    } else {
                        if (null != onSMSTimeListener) {
                            onSMSTimeListener.onReceiveSMS(FRString.valueOf(count));
                        }
                    }
                }
            });
        }
    }

    public void setOnSMSTimeListener(OnSMSTimeListener onSMSTimeListener) {
        this.onSMSTimeListener = onSMSTimeListener;
    }

    public interface OnSMSTimeListener {
        void onReceiveSMS(String second);

        void onNotReceiveSMS();
    }

    public void gcTimer(){
        if (null!=frTimer){
            frTimer.stop();
            frTimer = null;
        }
    }
}